package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.dao.*;
import com.spring.boot.entity.*;
import com.spring.boot.service.InsurancePolicyService;
import com.spring.boot.utils.ServiceException;
import com.spring.boot.vo.FindInsurancePolicyListVO;
import com.spring.boot.vo.FindVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Daniel ZTQ
 * @Date: 2021/01/01 22:59
 * @Description: 保单信息业务逻辑层 接口实现
 */
@Slf4j
@Service
public class InsurancePolicyServiceImpl extends ServiceImpl<InsurancePolicyMapper, InsurancePolicy> implements InsurancePolicyService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InsuredMapper insuredMapper;

    @Autowired
    private InsurancePolicyMapper insurancePolicyMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private CollectAndPayMapper collectAndPayMapper;

    /**
     * 查看保单列表（普通用户、管理员、财务人员）
     */
    @Override
    public Map<String, Object> findInsurancePolicyListByUid(FindInsurancePolicyListVO findInsurancePolicyListVO) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        //创建一个list对象
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //创建保单list对象
        List<InsurancePolicy> insurancePolicy = new ArrayList<>();

        try {
            //获取用户id
            String uid = findInsurancePolicyListVO.getUid();
            //获取用户角色
            String role = userMapper.getUserByUid(uid).getRole();
            //若为普通用户，则可查被保险人是自己的保单信息
            if ("2".equals(role)) {
                //通过uid获取身份证信息
                String idCard = userMapper.getUserByUid(uid).getIdCard();
                //前端传保单类型
                if (StringUtils.isNotBlank(findInsurancePolicyListVO.getState())) {
                    //获取前端传过来的类型
                    String state = findInsurancePolicyListVO.getState();
                    //根据被保险人身份证号和保单状态进行查询
                    insurancePolicy = insurancePolicyMapper.getInsurancePolicyByUidAndState(state, idCard);
                } else {
                    //如果没传保单类型
                    //根据身份证号,查看自己的保单信息
                    insurancePolicy = insurancePolicyMapper.getInsurancePolicyByinsuredIdCard(idCard);
                }
            } else {
                //前端传保单类型
                if (StringUtils.isNotBlank(findInsurancePolicyListVO.getState())) {
                    //获取前端传过来的类型
                    String state = findInsurancePolicyListVO.getState();
                    //根据被保险人身份证号和保单状态进行查询
                    insurancePolicy = insurancePolicyMapper.getInsurancePolicyAllAndState(state);
                } else {
                    //如果没传保单类型
                    //如果是管理员或财务人员 则可查全部保单信息
                    insurancePolicy = insurancePolicyMapper.getInsurancePolicyAll();
                }

            }

            //日志打印
            log.info("查看保单列表成功！");
            //设置状态码
            result.put("code", 200);
            //设置data值
            result.put("data", insurancePolicy);
            //设置msg值
            result.put("msg", "查看保单列表成功！");
            //设置status值
            result.put("status", true);
        } catch (Exception e) {
            //打印日志
            log.error("查看保单列表失败:", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "调用查看保单列表接口失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("查看保单列表接口失败！");
        }
        //返回数据
        return result;
    }

    /**
     * 被保险人发起理赔
     */
    @Override
    public Map<String, Object> updateInsurancePolicyByUuid(String insurancePolicyUuid) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        try {
            //判断参数不为空
            boolean blank = StringUtils.isNotBlank(insurancePolicyUuid);
            if (blank) {
                //根据保单uuid 修改保单状态，设置保单状态为2：发起理赔
                int i = insurancePolicyMapper.updateInsurancePolicyState(insurancePolicyUuid);
                if (i > 0) {
                    //日志打印
                    log.info("发起理赔成功！");
                    //设置状态码
                    result.put("code", 200);
                    //设置data值
                    result.put("data", "发起理赔成功");
                    //设置msg值
                    result.put("msg", "发起理赔成功！");
                    //设置status值
                    result.put("status", true);
                }
            } else {
                //打印日志
                log.error("保单uuid不为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "保单uuid不为空！");
                //设置status值
                result.put("status", false);
            }
        } catch (Exception e) {
            //打印日志
            log.error("发起理赔失败:", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "发起理赔失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("发起理赔失败！");
        }
        return result;
    }

    /**
     * 财务人员进行理赔
     */
    @Override
    public Map<String, Object> settlementOfClaims(String insurancePolicyUuid, String money) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        //创建对象
        CollectAndPay collectAndPay = new CollectAndPay();
        try {
            //判断入参不为空
            boolean blank = StringUtils.isNotBlank(insurancePolicyUuid);
            //不为空则正常进行
            if (blank) {
                //调用sql，修改保单状态
                int i = insurancePolicyMapper.settlementOfClaims(insurancePolicyUuid);
                //如果条数大于0，则修改保单状态成功
                if (i > 0) {
                    //查询该保单信息
                    InsurancePolicy insurancePolicy = insurancePolicyMapper.getInsurancePolicy(insurancePolicyUuid);
                    //获取保单id
                    Integer insurancePolicyId = insurancePolicy.getInsurancePolicyId();
                    //获取合同id
                    String contractId = insurancePolicy.getContractId();
                    //修改合同状态
                    int i1 = contractMapper.updateContractTypeById(contractId);
                    //如果修改成功
                    if (i1 > 0) {
                        //设置收付表的保单id
                        collectAndPay.setInsurancePolicyId(insurancePolicyId.toString());
                        //设置收付表的类型 1：支出
                        collectAndPay.setType("1");
                        //设置的时间
                        collectAndPay.setCreatedTime(new Date());
                        //设置金额
                        collectAndPay.setMoney(Double.parseDouble(money));
                        //调用insert方法 进行添加数据
                        collectAndPayMapper.insert(collectAndPay);
                        //日志打印
                        log.info("理赔成功！");
                        //设置状态码
                        result.put("code", 200);
                        //设置data值
                        result.put("data", "理赔成功");
                        //设置msg值
                        result.put("msg", "理赔成功！");
                        //设置status值
                        result.put("status", true);
                    } else {
                        //打印日志
                        log.error("理赔失败！");
                        //设置状态码
                        result.put("code", 300);
                        //设置msg值
                        result.put("msg", "理赔失败！");
                        //设置status值
                        result.put("status", false);
                    }
                } else {
                    //打印日志
                    log.error("理赔失败！");
                    //设置状态码
                    result.put("code", 300);
                    //设置msg值
                    result.put("msg", "理赔失败！");
                    //设置status值
                    result.put("status", false);
                }
            } else {
                //打印日志
                log.error("保单uuid不为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "保单uuid不为空！");
                //设置status值
                result.put("status", false);
            }

        } catch (Exception e) {
            //打印日志
            log.error("理赔失败:", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "理赔失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("理赔失败！");
        }
        return result;
    }


    /**
     * 综合查询 可根据投保人身份证号进行查询
     */
    @Override
    public Map<String, Object> getComprehensiveInquiry(FindVo findVo) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        //创建保单对象
        List<InsurancePolicy> insurancePolicyAll = new ArrayList<>();
        //创建一个list对象
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            //如果没有查询条件，则查询所有保单信息
            if (StringUtils.isBlank(findVo.getIdCard())) {
                //获取所有保单信息
                insurancePolicyAll = insurancePolicyMapper.getInsurancePolicyAll();
            } else {
                //如果有查询条件
                insurancePolicyAll = insurancePolicyMapper.getInsurancePolicyByIdCard(findVo.getIdCard());
            }
            //进行for循环遍历
            for (int i = 0; i < insurancePolicyAll.size(); i++) {
                //创建map对象
                Map<String, Object> resultData = new HashMap<>();
                //设置保单uuid
                resultData.put("insurancePolicyUuid", insurancePolicyAll.get(i).getInsurancePolicyUuid());
                //设置被保险人身份证号
                resultData.put("insuredIdCard", insurancePolicyAll.get(i).getInsuredIdCard());
                //设置险种名称
                resultData.put("insuranceName", insurancePolicyAll.get(i).getInsuranceName());
                //设置保单状态
                resultData.put("state", insurancePolicyAll.get(i).getState());
                //设置保单生效时间
                resultData.put("entryIntoForceTime", insurancePolicyAll.get(i).getEntryIntoForceTime());
                //设置保单失效时间
                resultData.put("expirationTime", insurancePolicyAll.get(i).getExpirationTime());
                //设置合同id
                resultData.put("contractUuid", insurancePolicyAll.get(i).getContractUuid());

                //获取被保险人id
                String insuredId = insurancePolicyAll.get(i).getInsuredId();
                //获取被保险人信息
                Insured insured = insuredMapper.selectById(insuredId);
                //获取投保人id
                String identityCard = insured.getIdentityCard();
                //设置投保人身份证号
                resultData.put("identityCard", identityCard);
                //将数据添加到list里面
                list.add(resultData);
            }
            //日志打印
            log.info("综合查询成功！");
            //设置状态码
            result.put("code", 200);
            //设置data值
            result.put("data", list);
            //设置msg值
            result.put("msg", "综合查询成功！");
            //设置status值
            result.put("status", true);
        } catch (Exception e) {
            //打印日志
            log.error("综合查询失败:", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "综合查询失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("综合查询失败！");
        }
        return result;
    }
}
